#include "ByteStream.h"
#include <algorithm>
#include <stdexcept>

#include "Debug.h"

CByteStream::CByteStream(void)
{
}

CByteStream::CByteStream( const CByteStream& other )
{
	std::string otherBuffer = other.getBufferContent();
	m_VecBuffer.resize(otherBuffer.size());
	std::copy(otherBuffer.begin(), otherBuffer.end(), m_VecBuffer.begin());
}


std::string CByteStream::getBufferContent() const
{
	return std::string(m_VecBuffer.begin(), m_VecBuffer.end());
}

void CByteStream::addBack( const std::string& data )
{
	addAt(data, m_VecBuffer.size());
}

void CByteStream::addFront( const std::string& data )
{
	addAt(data, 0);
}

void CByteStream::addAt( const std::string& data, int offset )
{
	m_VecBuffer.insert(m_VecBuffer.begin() + offset, data.begin(), data.end());
}

CByteStream& CByteStream::operator=( const CByteStream& other )
{
	TRACE(__FUNCTION__, __LINE__, " operator = for CByteStream");

	std::string otherBuffer = other.getBufferContent();
	m_VecBuffer.resize(otherBuffer.size());
	std::copy(otherBuffer.begin(), otherBuffer.end(), m_VecBuffer.begin());
	return *this;
}

void CByteStream::removeElements(int offset, int count )
{
	if( offset >= 0 && offset < (int)m_VecBuffer.size() && offset + count >= 0 && offset + count <= (int)m_VecBuffer.size() ) 
	{
		m_VecBuffer.erase(m_VecBuffer.begin() + offset, m_VecBuffer.begin() + offset + count);		
	}
	else 
	{
		throw new std::logic_error("Index out of bounds");
	}
}

BOOST_CLASS_EXPORT(CByteStream)
